package com.sun.project.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.sun.common.result.ActionResult;
import com.sun.common.result.SupportResult;
import com.sun.common.util.SequenceGenerator;
import com.sun.project.model.ProductModel;
import com.sun.project.model.ProjectModel;
import com.sun.project.model.ProjectProductModel;
import com.sun.project.service.ProductService;
import com.sun.project.service.ProjectProductService;
import com.sun.project.service.ProjectService;
import com.sun.project.vo.ProjectSetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.hutool.core.bean.BeanUtil.copyProperties;
import static java.util.stream.Collectors.toList;


//项目操作

@RestController
@RequestMapping("/project")
public class ProjectController {

    private static SequenceGenerator sequenceGenerator = new SequenceGenerator();

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProjectProductService projectProductService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public SupportResult list(@RequestBody ProjectModel projectModel) {
        int currentPage = projectModel.getCurrentPage()==null?1:projectModel.getCurrentPage();
        int pageSize = projectModel.getPageSize()==null?10:projectModel.getPageSize();
        MPJQueryWrapper<ProjectModel> wrapperM=new MPJQueryWrapper<ProjectModel>()
                .selectAll(ProjectModel.class);

        String parent = projectModel.getParent();
        if(StrUtil.isNotBlank(parent)) {
            wrapperM.eq("t.parent",parent);
        }else{
            wrapperM.eq("t.parent","0");
        }

        String status = projectModel.getStatus();
        if(StrUtil.isNotBlank(status)) {
            wrapperM.eq("t.status",status);
        }

        //调用方法实现分页查询
        IPage<ProjectModel> projectModelPage = projectService.selectJoinListPage(
                new Page<ProjectModel>(currentPage,pageSize),ProjectModel.class,wrapperM);

        SupportResult rs=new SupportResult(projectModelPage);

        rs.setTotalResult((int)projectModelPage.getTotal());

        return rs;
    }


    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public SupportResult getById(@RequestBody ProjectModel projectModelParam) {
        ProjectModel   projectModel=  projectService.getById(projectModelParam.getId());
        if(!StrUtil.equals(projectModel.getProduct(),"0")){
            MPJQueryWrapper<ProjectProductModel> wrapperM=new MPJQueryWrapper<ProjectProductModel>()
                    .selectAll(ProjectProductModel.class)
                    .leftJoin("zt_project t1 on t.project =t1.id")
                    .eq("t.project",projectModelParam.getId())
                    .ne("t1.parent","0");
            ProjectProductModel ppm =projectProductService.selectJoinOne(ProjectProductModel.class,wrapperM);
            projectModel.setProduct(ppm.getProduct()+"");
        }

        SupportResult rs=new SupportResult(projectModel);

        return rs;
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public  ActionResult saveOrUpdate(@RequestBody ProjectModel projectModelParam) {
        if(StrUtil.isNotBlank(projectModelParam.getId())){
            projectService.updateById(projectModelParam);
        }else{
            String id=sequenceGenerator.nextStringId();
            projectModelParam.setId(id);
            projectModelParam.setOutput("");
            projectService.save(projectModelParam);
            if(StrUtil.isNotBlank(projectModelParam.getProduct())){
                String product=projectModelParam.getProduct();
                ProjectProductModel ppm=new ProjectProductModel();
                ppm.setProject(id);
                ppm.setProduct(product);
                projectProductService.save(ppm);
            }
        }
        return ActionResult.success();
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public  ActionResult delete(@RequestBody ProjectModel projectModelParam) {
        projectService.removeById(projectModelParam.getId());
        return ActionResult.success();
    }


    //项目集下拉
    @RequestMapping(value = "/setSelect", method = RequestMethod.POST)
    public SupportResult setSelect() {
        List<ProjectModel> list = projectService.lambdaQuery().eq(ProjectModel::getParent, "0").ne(ProjectModel::getStatus,"closed").list();
        SupportResult rs=new SupportResult(list);
        return rs;
    }


    //项目集看板统计
    @RequestMapping(value = "/setKanban", method = RequestMethod.POST)
    public SupportResult setKanban() {
        List<ProjectSetVo> setList = projectService.lambdaQuery().eq(ProjectModel::getParent, "0").list().stream().map(v->copyProperties(v, ProjectSetVo.class)).collect(toList());
        setList.stream().forEach(s->{
                String setId=s.getId();
            List<ProjectModel> items = projectService.lambdaQuery().eq(ProjectModel::getParent, setId).list();
                s.setItems(items);
            MPJQueryWrapper<ProductModel> wrapper=new MPJQueryWrapper<ProductModel>()
                    .selectAll(ProductModel.class)
                    .leftJoin("zt_projectproduct t1 on t.id=t1.product")
                    .eq("t1.project",setId);

            List<ProductModel> products = productService.selectJoinList(ProductModel.class,wrapper);
                s.setProducts(products);
        });

        SupportResult rs=new SupportResult(setList);
        return rs;
    }

}
